package com.lucian.ge.controller.shopportal;

import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.auth.controller.GetAuthController;
import com.lucian.ge.comman.model.ResultMap;
import com.lucian.ge.comman.model.ResultMapSyn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

/**
 * Created by Lucicol on 2019/8/5.
 */
@Api(value = "QrcodeController", description = "Qrcode-小程序码接口")

@RestController
@RequestMapping("/qrcode")
public class QrcodeController {
    private final ResultMap resultMap;

    @Autowired
    public QrcodeController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    /**
     * 获得小程序码
     */
    @ApiOperation(value="获得小程序码", notes="")

    @PostMapping("/get_qrcode")
    @ResponseBody
    public String get_qrcode (@RequestParam(value = "userid") String userId,@RequestParam(value = "id") String id) {

        Map<String, String> request = new HashMap<String, String>();
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa13b77d438fbf138&secret=ea9e4046d683d8ba429b08c5113ab0cc" , request , String.class);
//        System.out.println(result);
        JSONObject jsonObject = JSONObject.parseObject(result);
//        System.out.println(jsonObject.getString("access_token"));

        Map<String, String> request2 = new HashMap<String, String>();
        request2.put("scene", "shareid=" + userId + "&id=" + id);
        request2.put("page", "pages/product-detail/main");
//        request2.put("scene", userId);
//        System.out.println(request2);

        RestTemplate restTemplate2 = new RestTemplate();
        byte[] result2 = restTemplate2.postForObject("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+jsonObject.getString("access_token"), request2 , byte[].class);
//        System.out.println(Base64.encodeBase64String(result2));
        Base64.encodeBase64String(result2);
        return Base64.encodeBase64String(result2);
//        return resultMap.success().code(200).message("获得小程序码成功").token("").res(result2).data(null);
    }
}
